{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "seasonal-balance",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "# '0ba8feef618e5db7b1ebb65538fe51e4aef69fb3cbf709d44128f313'\n",
    "# https://waditu.com/document/2?doc_id=25\n",
    "ts.set_token('0ba8feef618e5db7b1ebb65538fe51e4aef69fb3cbf709d44128f313')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fifteen-brazilian",
   "metadata": {},
   "outputs": [],
   "source": [
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "subjective-release",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>exchange</th>\n",
       "      <th>cal_date</th>\n",
       "      <th>is_open</th>\n",
       "      <th>pretrade_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180901</td>\n",
       "      <td>0</td>\n",
       "      <td>20180831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180902</td>\n",
       "      <td>0</td>\n",
       "      <td>20180831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180908</td>\n",
       "      <td>0</td>\n",
       "      <td>20180907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180909</td>\n",
       "      <td>0</td>\n",
       "      <td>20180907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180915</td>\n",
       "      <td>0</td>\n",
       "      <td>20180914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180916</td>\n",
       "      <td>0</td>\n",
       "      <td>20180914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180922</td>\n",
       "      <td>0</td>\n",
       "      <td>20180921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180923</td>\n",
       "      <td>0</td>\n",
       "      <td>20180921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180924</td>\n",
       "      <td>0</td>\n",
       "      <td>20180921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180929</td>\n",
       "      <td>0</td>\n",
       "      <td>20180928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20180930</td>\n",
       "      <td>0</td>\n",
       "      <td>20180928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>SSE</td>\n",
       "      <td>20181001</td>\n",
       "      <td>0</td>\n",
       "      <td>20180928</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   exchange  cal_date  is_open pretrade_date\n",
       "0       SSE  20180901        0      20180831\n",
       "1       SSE  20180902        0      20180831\n",
       "2       SSE  20180908        0      20180907\n",
       "3       SSE  20180909        0      20180907\n",
       "4       SSE  20180915        0      20180914\n",
       "5       SSE  20180916        0      20180914\n",
       "6       SSE  20180922        0      20180921\n",
       "7       SSE  20180923        0      20180921\n",
       "8       SSE  20180924        0      20180921\n",
       "9       SSE  20180929        0      20180928\n",
       "10      SSE  20180930        0      20180928\n",
       "11      SSE  20181001        0      20180928"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pro.query('trade_cal', exchange='', start_date='20180901', end_date='20181001', fields='exchange,cal_date,is_open,pretrade_date', is_open='0')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "detected-adobe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# res = df.to_sql('stock_basic', engine_ts, index=False, if_exists='append', chunksize=5000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "commercial-frank",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       ts_code           trade_time   open  close   high    low      vol  \\\n",
      "0    600000.SH  2020-01-08 15:00:00  12.32  12.32  12.32  12.32  1485837   \n",
      "1    600000.SH  2020-01-08 14:59:00  12.32  12.32  12.32  12.32        0   \n",
      "2    600000.SH  2020-01-08 14:58:00  12.32  12.32  12.32  12.32    22000   \n",
      "3    600000.SH  2020-01-08 14:57:00  12.32  12.32  12.33  12.32   266196   \n",
      "4    600000.SH  2020-01-08 14:56:00  12.32  12.32  12.33  12.32   265829   \n",
      "..         ...                  ...    ...    ...    ...    ...      ...   \n",
      "477  600000.SH  2020-01-07 09:34:00  12.55  12.55  12.57  12.55   406300   \n",
      "478  600000.SH  2020-01-07 09:33:00  12.51  12.55  12.55  12.50   427336   \n",
      "479  600000.SH  2020-01-07 09:32:00  12.51  12.50  12.52  12.50   350500   \n",
      "480  600000.SH  2020-01-07 09:31:00  12.51  12.50  12.51  12.50   335700   \n",
      "481  600000.SH  2020-01-07 09:30:00  12.51  12.51  12.51  12.51   346000   \n",
      "\n",
      "         amount trade_date  pre_close  \n",
      "0    18305512.0   20200108      12.32  \n",
      "1           0.0   20200108      12.32  \n",
      "2      271040.0   20200108      12.32  \n",
      "3     3280959.0   20200108      12.32  \n",
      "4     3275157.0   20200108      12.32  \n",
      "..          ...        ...        ...  \n",
      "477   5102477.0   20200107      12.55  \n",
      "478   5351921.0   20200107      12.50  \n",
      "479   4385232.0   20200107      12.50  \n",
      "480   4198674.0   20200107      12.51  \n",
      "481   4328460.0   20200107        NaN  \n",
      "\n",
      "[482 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "df = ts.pro_bar(ts_code='600000.SH',\n",
    "                    freq='1min', \n",
    "                    start_date='2020-01-07 09:00:00', \n",
    "                    end_date='2020-01-08 17:00:00')\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fancy-questionnaire",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "agreed-relay",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>000001</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>深圳</td>\n",
       "      <td>银行</td>\n",
       "      <td>19910403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>000002</td>\n",
       "      <td>万科A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>全国地产</td>\n",
       "      <td>19910129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>000004</td>\n",
       "      <td>国华网安</td>\n",
       "      <td>深圳</td>\n",
       "      <td>互联网</td>\n",
       "      <td>19910114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000005.SZ</td>\n",
       "      <td>000005</td>\n",
       "      <td>世纪星源</td>\n",
       "      <td>深圳</td>\n",
       "      <td>环境保护</td>\n",
       "      <td>19901210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>000006</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>区域地产</td>\n",
       "      <td>19920427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4198</th>\n",
       "      <td>688777.SH</td>\n",
       "      <td>688777</td>\n",
       "      <td>中控技术</td>\n",
       "      <td>浙江</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20201124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4199</th>\n",
       "      <td>688788.SH</td>\n",
       "      <td>688788</td>\n",
       "      <td>科思科技</td>\n",
       "      <td>深圳</td>\n",
       "      <td>通信设备</td>\n",
       "      <td>20201022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4200</th>\n",
       "      <td>688819.SH</td>\n",
       "      <td>688819</td>\n",
       "      <td>天能股份</td>\n",
       "      <td>浙江</td>\n",
       "      <td>电气设备</td>\n",
       "      <td>20210118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4201</th>\n",
       "      <td>688981.SH</td>\n",
       "      <td>688981</td>\n",
       "      <td>中芯国际-U</td>\n",
       "      <td>上海</td>\n",
       "      <td>半导体</td>\n",
       "      <td>20200716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4202</th>\n",
       "      <td>689009.SH</td>\n",
       "      <td>689009</td>\n",
       "      <td>九号公司-UWD</td>\n",
       "      <td>北京</td>\n",
       "      <td>专用机械</td>\n",
       "      <td>20201029</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4203 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code  symbol      name area industry list_date\n",
       "0     000001.SZ  000001      平安银行   深圳       银行  19910403\n",
       "1     000002.SZ  000002       万科A   深圳     全国地产  19910129\n",
       "2     000004.SZ  000004      国华网安   深圳      互联网  19910114\n",
       "3     000005.SZ  000005      世纪星源   深圳     环境保护  19901210\n",
       "4     000006.SZ  000006      深振业A   深圳     区域地产  19920427\n",
       "...         ...     ...       ...  ...      ...       ...\n",
       "4198  688777.SH  688777      中控技术   浙江     软件服务  20201124\n",
       "4199  688788.SH  688788      科思科技   深圳     通信设备  20201022\n",
       "4200  688819.SH  688819      天能股份   浙江     电气设备  20210118\n",
       "4201  688981.SH  688981    中芯国际-U   上海      半导体  20200716\n",
       "4202  689009.SH  689009  九号公司-UWD   北京     专用机械  20201029\n",
       "\n",
       "[4203 rows x 6 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "domestic-prophet",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
